<?php
/**
 * Author : CuongNV
 */
class RoomsController extends AppController {

	public function index() {
		$this->loadModel('Cateroom');
		$caterooms = $this->Cateroom->find('list',array(
			'fields' => array('Cateroom.id', 'Cateroom.name'),
			'order' => array('Cateroom.order DESC')
		));
		$this->loadModel('Storey');
		$storeys = $this->Storey->find('list', array(
			'fields' => array('Storey.id', 'Storey.type'),
			'order' => array('Storey.id' => 'ASC','Storey.order' => 'ASC')
		));
		$this->set(compact('caterooms', 'storeys'));
		$this->Room->recursive = 0;
		$this->Paginator->settings = array('order' => array('Room.storey_id' => 'ASC'),
			'contain' => array('Caterooms'));
		$this->set('rooms', $this->Paginator->paginate('Room'));
	}

	public function get_first(){
		$this->autoRender = false;
		$data = json_decode($_GET["id"]);
		$result = $this->Room->find('first',array(
			'conditions' => array('Room.id' => $data->key),
			'fields' => array('Room.id', 'Room.caterooms_id', 'Room.name', 'Room.description',
				'Room.count', 'Room.count_people', 'Room.storey_id')
		));
		echo json_encode($result);
	}

	public function add() {
		if ($this->request->is('post')) {
			$this->request->data['Room']['description'] = trim($this->request->data['Room']['description']);
			if($this->request->data['Room']['max_people'] == '') {
				$this->request->data['Room']['max_people'] = $this->request->data['Room']['count_people'];
			}
			$this->Room->create();
			$this->Room->save($this->request->data);
			return $this->redirect(array('controller'=>'rooms', 'action' => 'index'));
		}else{
			$caterooms = $this->Room->Caterooms->find('list');
			$storeys = $this->Room->Storey->find('list');
			$this->set(compact('caterooms', 'storeys'));
		}
	}
	public function setup_price(){
		$this->loadModel('Cateroom');
		$this->Cateroom->recursive = 1;
		$data = $this->Cateroom->find('all');
		foreach ($data as $value) {
			$arr_cate [] = $value['Cateroom'];
			foreach ($value['Room'] as $val) {
				$arr_room [] = $val;
			}
		}
		$this->set(compact('arr_cate', 'arr_room'));
	}
	public function list_room_full(){
		$this->autoRender = false;
		$result = $this->Room->find('list', array('order' => array('Room.storey_id' => 'ASC')));
		echo json_encode($result);
	}

	public function sto_cate(){
		$this->autoRender = false;
		$this->loadModel('Cateroom');
		$this->loadModel('Storey');
		$this->Cateroom->recursive = -1;
		$this->Storey->recursive = -1;
		$cate = $this->Cateroom->find('list');
		$stor = $this->Storey->find('list');
		$array = array('1'=>$cate,'2'=>$stor);
		return json_encode($array);
	}

	public function edit($id = null) {
		if (!$this->Room->exists($id)) {
			return $this->redirect(array('controller' => 'rooms', 'action' => 'index'));
		}
		if ($this->request->is(array('post', 'put'))) {
			$this->request->data['Room']['description'] = trim($this->request->data['Room']['description']);
			if($this->request->data['Room']['max_people'] == '') {
				$this->request->data['Room']['max_people'] = $this->request->data['Room']['count_people'];
			}
			$this->request->data['Room']['id'] = $id;
			$this->Room->save($this->request->data);
			return $this->redirect(array('controller' => 'rooms', 'action' => 'index'));
		}else {
			$this->Room->recursive = -1;
			$options = array('conditions' => array('Room.' . $this->Room->primaryKey => $id));
			$this->request->data = $this->Room->find('first', $options);
			$caterooms = $this->Room->Caterooms->find('list');
			$storeys = $this->Room->Storey->find('list');;
			$this->set(compact('caterooms', 'storeys', 'peo'));
		}
	}

	public function information($id = null){
		$data = $this->Room->find('first', array('conditions' => array('Room.id' => $id)));
		$this->set(compact('data'));
	}

	public function change(){
		$this->autoRender = false;
		$data = json_decode($_GET["data"]);
		$array_id = explode(',', $data->arr_key);
		$count_arr_id = count($array_id);
		$location = $data->local;
		$type = $data->type;
		if($location != ""){
			$this->request->data['Room']['storey_id'] = $location;
		}
		if($type != ""){
			$this->request->data['Room']['caterooms_id'] = $type;
		}
		foreach ($array_id as $key => $value) {
			$this->Room->id = $value;
			$this->Room->save($this->request->data);
		}
	}

	public function price(){
		$this->loadModel('Cateroom');
		$this->Cateroom->recursive = 1;
		$data = $this->Cateroom->find('all');
		$i = 0;
		foreach ($data as $value1) {
			foreach ($value1['Room'] as $value2) {
				$arr[$i]['room'] = $value2;
				$arr[$i]['price_date'] = $value1['Cateroom'];
				$arr[$i]['price_hour'] = $value1['PriceTime'];
				$i++;
			}
		}
		$this->set('data',$arr);
	}
	
	public function delete($id = null) {
		$this->Room->id = $id;
		if (!$this->Room->exists()) {
			return $this->redirect(array('controller'=>'rooms','action' => 'index'));
		}
		$this->loadModel('Reservation');
		$check = $this->Reservation->find("first", array(
			"conditions" => array("OR" => array("Reservation.rooms_id" => $id, 
								'FIND_IN_SET(\''. $id .'\',Reservation.rooms_id)' )
			))
		);
		if($check){
			$link = "http://".$_SERVER["HTTP_HOST"].$this->webroot."rooms";
			echo '<script>';
			echo 'alert("Phòng đã tồn tại trong bảng đặt phòng. Không thể xóa !");';
			echo "window.location.href = '<?php echo $link; ?>' ";
			echo '</script>';
		}else{
			$this->Room->delete();
			$this->redirect(array('controller'=>'rooms','action' => 'index'));
		}
	}


}
